package com.nexfra.bbssys.adm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nexfra.bbssys.adm.service.AdmBoardMngrService;
import com.nexfra.bbssys.common.Constants;
import com.nexfra.bbssys.common.service.CommonService;
import com.nexfra.bbssys.common.utils.StringUtil;

@Controller
@RequestMapping(value = "/adm/admBoardMngr") 
public class AdmBoardMngrController {
	private static final Logger logger = LoggerFactory.getLogger(AdmBoardMngrController.class);
	
	@Autowired
	AdmBoardMngrService service;

	@Autowired
	CommonService commonService;

	/**
	 * 게시판목록을 조회한다.
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "")
	public String selectBoardList(
			@RequestParam HashMap<String, String> params,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model)  throws Exception {
		List<Map<String, Object>> list = service.selectBoardList(params);
		logger.info("list" + list);

		model.addAttribute("list", list);
		return "/bbssys/adm/admBoardMngr";
	}	

	/**
	 * 게시판을 조회한다.
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "selectBoardOne")
	public String selectBoardOne(
			@RequestParam HashMap<String, String> params,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model)  throws Exception {
		Map<String, Object> item = service.selectBoardOne(params);
		logger.info("item" + item);

		model.addAttribute("item", item);
		return "/bbssys/adm/admBoardMngrDetail";
	}	
	
	/**
	 * 게시판을 등록한다.
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/registBoard")
	@Transactional(rollbackFor = Exception.class)  
	public String registBoard(
			@RequestParam HashMap<String, String> params,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception {
		logger.info("params:"+params.toString());
		logger.info("request.getParameterMap:"+request.getParameterMap());
  
		// 체크박스값 변환
		params.put("atch_attr_yn" , params.get("atch_attr_yn" )==null?"N":"Y");
		params.put("reply_attr_yn", params.get("reply_attr_yn")==null?"N":"Y");
		params.put("cmmt_attr_yn" , params.get("cmmt_attr_yn" )==null?"N":"Y");
		params.put("tag_attr_yn"  , params.get("tag_attr_yn"  )==null?"N":"Y");
		params.put("rcmd_attr_yn" , params.get("rcmd_attr_yn" )==null?"N":"Y");
		params.put("cate_attr_yn" , params.get("cate_attr_yn" )==null?"N":"Y");
		params.put("hidd_attr_yn" , params.get("hidd_attr_yn" )==null?"N":"Y");
		params.put("atcl_dt_attr_yn" , params.get("atcl_dt_attr_yn" )==null?"N":"Y");
		// 날짜값 변환
		params.put("view_stdt", StringUtil.replace((String)params.get("view_stdt" ), "/", ""));
		params.put("view_eddt", StringUtil.replace((String)params.get("view_eddt" ), "/", ""));
		
		params.put("user_id", "admin");
//@TODO null에 User객체 넘기기.....		
		service.registBoard(params, null);

		// 조회
		List<Map<String, Object>> list = service.selectBoardList(params);
		logger.info("list" + list);
		model.addAttribute("list", list);
		
		return "/bbssys/adm/admBoardMngr";
	}	
	
	/**
	 * 게시판을 수정한다.
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updateBoard")
	@Transactional(rollbackFor = Exception.class)  
	public String updateBoard(
			@RequestParam HashMap<String, String> params,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception {
		logger.info("params:"+params.toString());
		logger.info("request.getParameterMap:"+request.getParameterMap());
		
		// 체크박스값 변환
		params.put("atch_attr_yn" , params.get("atch_attr_yn" )==null?"N":"Y");
		params.put("reply_attr_yn", params.get("reply_attr_yn")==null?"N":"Y");
		params.put("cmmt_attr_yn" , params.get("cmmt_attr_yn" )==null?"N":"Y");
		params.put("tag_attr_yn"  , params.get("tag_attr_yn"  )==null?"N":"Y");
		params.put("rcmd_attr_yn" , params.get("rcmd_attr_yn" )==null?"N":"Y");
		params.put("cate_attr_yn" , params.get("cate_attr_yn" )==null?"N":"Y");
		params.put("hidd_attr_yn" , params.get("hidd_attr_yn" )==null?"N":"Y");
		params.put("atcl_dt_attr_yn" , params.get("atcl_dt_attr_yn" )==null?"N":"Y");
		// 날짜값 변환
		params.put("view_stdt", StringUtil.replace((String)params.get("view_stdt" ), "/", ""));
		params.put("view_eddt", StringUtil.replace((String)params.get("view_eddt" ), "/", ""));
		
		params.put("user_id", "admin");
		service.updateBoard(params, null);
		
		// 조회
		List<Map<String, Object>> list = service.selectBoardList(params);
		logger.info("list" + list);
		model.addAttribute("list", list);
		
		return "/bbssys/adm/admBoardMngr";
	}	
	
	/**
	 * 게시판을 삭제한다.
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/deleteBoard")
	@Transactional(rollbackFor = Exception.class)  
	public String deleteBoard(
			@RequestParam HashMap<String, String> params,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception {
		logger.info("params:"+params.toString());
		logger.info("request.getParameterMap:"+request.getParameterMap());
		
		service.deleteBoard(params, null);
		
		// 조회
		List<Map<String, Object>> list = service.selectBoardList(params);
		logger.info("list" + list);
		model.addAttribute("list", list);
		
		return "/bbssys/adm/admBoardMngr";
	}	
	
	
///////////////////////////////////////////	
	
	/**
	 * VOC등록 - 본인 처리
	 * @param model
	 * @return
	 */
	/*@RequestMapping(value = "/saveSelf")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Map<String,Object> saveSelf(@RequestParam HashMap<String, String> params
			,HttpServletRequest request, HttpServletResponse response
			,ModelMap model) throws Exception {
		logger.info("requestParam:"+params.toString());
		logger.info("requestParameterMap:"+request.getParameterMap());
		
		MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest)request;
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute(Constants.USER_KEY);
				
		// file test
	    Iterator<String> files1 =  mpRequest.getFileNames();
	    
		while(files1.hasNext()) {	
			MultipartFile mpf = mpRequest.getFile(files1.next());
			
			logger.info("===================================");
			logger.info(""+mpf.getContentType());
			logger.info(""+mpf.getOriginalFilename());
			logger.info(""+mpf.getSize());
			logger.info(""+mpf.getName());
		}
		
		Map<String, MultipartFile> files = mpRequest.getFileMap();
		List<AttachFile> uploadedAttachFiles = FileManagement.uploadFile(mpRequest, "VOC", files, null); // 저장될 위치,MultipartFile 리스트,저장될 파일이름,제한할 파일사이즈(10485760B=>10MB)
		
		logger.info(uploadedAttachFiles + "");
		
		service.saveSelf(params, user, uploadedAttachFiles);
		
		// voc id에 해당하는 첨부파일 조회
		List<AttachFile> attachFiles = commonService.selectAttachFile(params);		
		
		model.addAttribute("message", Constants.SUCCESS_MESSAGE)
		 .addAttribute("attachFiles", attachFiles);
		return model;
	}	*/

	/**
	 * VOC등록 - 접수요청
	 * @param model
	 * @return
	 */
	/*@RequestMapping(value = "/saveReq")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	//public String saveReq(@RequestParam HashMap<String, String> params
	public Map<String,Object> saveReq(@RequestParam HashMap<String, String> params
			,HttpServletRequest request, HttpServletResponse response
			,ModelMap model) throws Exception {
		logger.info("requestParam:"+params.toString());
		logger.info("requestParameterMap:"+request.getParameterMap());
		
		MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest)request;
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute(Constants.USER_KEY);
		
		// file test
		Iterator<String> files1 =  mpRequest.getFileNames();
		
		while(files1.hasNext()) {	
			MultipartFile mpf = mpRequest.getFile(files1.next());
			
			logger.info("===================================");
			logger.info(""+mpf.getContentType());
			logger.info(""+mpf.getOriginalFilename());
			logger.info(""+mpf.getSize());
			logger.info(""+mpf.getName());
		}
		
		Map<String, MultipartFile> files = mpRequest.getFileMap();
		List<AttachFile> uploadedAttachFiles = FileManagement.uploadFile(mpRequest, "VOC", files, null); // 저장될 위치,MultipartFile 리스트,저장될 파일이름,제한할 파일사이즈(10485760B=>10MB)
		
		logger.info(uploadedAttachFiles + "");
		
		service.saveReq(params, user, uploadedAttachFiles);


		// voc id에 해당하는 첨부파일 조회
		List<AttachFile> attachFiles = commonService.selectAttachFile(params);		
		
		model.addAttribute("message", Constants.SUCCESS_MESSAGE)
		 .addAttribute("attachFiles", attachFiles);
		return model; 
	}*/	  

}